package pl.edu.project.view;

import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

@SuppressWarnings("serial")
public class ImportExportWindow extends JDialog {

    private class ActionCancel implements ActionListener {
	public void actionPerformed(ActionEvent e) {
	    dispose();
	}
    }
    private class ActionChooseFile implements ActionListener {
	public void actionPerformed(ActionEvent e) {
	    int chosenFile;
	    if (isImport)
		chosenFile = fileChooser
			.showOpenDialog(ImportExportWindow.this);
	    else
		chosenFile = fileChooser
			.showSaveDialog(ImportExportWindow.this);
	    if (chosenFile == JFileChooser.APPROVE_OPTION) {
		if (e.getSource() == xmlFileChooserButton)
		    xmlFile = fileChooser.getSelectedFile();
		else
		    outlookFile = fileChooser.getSelectedFile();
	    }
	}
    }
    private class ActionOK implements ActionListener {
	public void actionPerformed(ActionEvent e) {
	    dispose();
	    // dopisac
	}
    }
    private class ChangeIo implements ChangeListener {
	public void stateChanged(ChangeEvent e) {
	    AbstractButton button = (AbstractButton) e.getSource();
	    if (button.getModel().isPressed()) {
		if (button.getModel().isSelected()) {
		    if (e.getSource() == databaseCheckBox) {
			ioDatabase = true;
			if (isImport) {
			    xmlCheckBox.setEnabled(false);
			    outlookCheckBox.setEnabled(false);
			}
		    }
		    if (e.getSource() == xmlCheckBox) {
			ioXML = true;
			if (isImport) {
			    databaseCheckBox.setEnabled(false);
			    outlookCheckBox.setEnabled(false);
			}
		    }
		    if (e.getSource() == outlookCheckBox) {
			ioOutlook = true;
			if (isImport) {
			    xmlCheckBox.setEnabled(false);
			    databaseCheckBox.setEnabled(false);
			}
		    }

		} else if (!button.getModel().isSelected()) {
		    if (e.getSource() == databaseCheckBox) {
			ioDatabase = false;
			if (isImport) {
			    xmlCheckBox.setEnabled(true);
			    outlookCheckBox.setEnabled(true);
			}
		    }
		    if (e.getSource() == xmlCheckBox) {
			ioXML = false;
			if (isImport) {
			    databaseCheckBox.setEnabled(true);
			    outlookCheckBox.setEnabled(true);
			}
		    }
		    if (e.getSource() == outlookCheckBox) {
			ioOutlook = false;
			if (isImport) {
			    xmlCheckBox.setEnabled(true);
			    databaseCheckBox.setEnabled(true);
			}
		    }
		}
	    }
	}
    }
    private JButton cancelButton;
    private JCheckBox databaseCheckBox;
    private JLabel databaseLabel;
    private JFileChooser fileChooser;
    private JLabel infoLabel;
    private boolean ioDatabase;
    private boolean ioOutlook;
    private boolean ioXML;
    private boolean isImport;
    private CalendarFrame mainWindow;
    /**
	 * 
	 */

    private JButton okButton;
    private JCheckBox outlookCheckBox;
    private File outlookFile;

    private JButton outlookFileChooserButton;
    private JLabel outlookLabel;

    private JCheckBox xmlCheckBox;

    private File xmlFile;

    private JButton xmlFileChooserButton;

    private JLabel xmlLabel;

    public ImportExportWindow(Boolean isImport, JFrame owner) {
	super(owner, "Eksportuj zdarzenia");
	setSize(260, 200);
	setLocation(owner.getLocation().x + 45, owner.getLocation().y + 115);
	setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	// this.mainWindow = (CallendarFrame) owner;
	this.isImport = isImport;
	if (this.isImport)
	    this.setTitle("Importuj zdarzenia");

	setData();
	// layout
	Container contentPane = getContentPane();
	contentPane.setLayout(new GridBagLayout());
	GridBagConstraints c = new GridBagConstraints();
	c.fill = GridBagConstraints.HORIZONTAL;
	c.insets.set(10, 5, 5, 5);
	c.gridwidth = 3;
	c.gridy = 0;
	c.gridx = 0;
	add(infoLabel, c);
	c.insets.set(1, 5, 1, 5);
	c.gridwidth = 1;
	c.gridy = 1;
	add(databaseLabel, c);
	c.gridx = 1;
	add(databaseCheckBox, c);
	c.gridy = 2;
	c.gridx = 0;
	add(xmlLabel, c);
	c.gridx = 1;
	add(xmlCheckBox, c);
	c.gridx = 2;
	add(xmlFileChooserButton, c);
	c.gridy = 3;
	c.gridx = 0;
	add(outlookLabel, c);
	c.gridx = 1;
	add(outlookCheckBox, c);
	c.gridx = 2;
	add(outlookFileChooserButton, c);
	c.gridwidth = 3;
	c.insets.set(10, 5, 5, 5);
	c.gridy = 4;
	c.gridx = 0;
	JPanel okCancel = new JPanel();
	okCancel.setAlignmentX(RIGHT_ALIGNMENT);
	okCancel.add(okButton);
	okCancel.add(cancelButton);
	add(okCancel, c);

	setResizable(false);
	setVisible(true);
    }

    private void setData() {
	this.okButton = new JButton("OK");
	okButton.addActionListener(new ActionOK());
	okButton.registerKeyboardAction(new ActionOK(),
		KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
		JComponent.WHEN_IN_FOCUSED_WINDOW);
	this.cancelButton = new JButton("Anuluj");
	cancelButton.addActionListener(new ActionCancel());
	cancelButton.registerKeyboardAction(new ActionCancel(),
		KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false),
		JComponent.WHEN_IN_FOCUSED_WINDOW);
	this.databaseCheckBox = new JCheckBox();
	databaseCheckBox.addChangeListener(new ChangeIo());
	this.xmlCheckBox = new JCheckBox();
	xmlCheckBox.addChangeListener(new ChangeIo());
	this.outlookCheckBox = new JCheckBox();
	outlookCheckBox.addChangeListener(new ChangeIo());
	this.fileChooser = new JFileChooser();
	this.xmlFileChooserButton = new JButton("Wybierz plik");
	xmlFileChooserButton.addActionListener(new ActionChooseFile());
	this.outlookFileChooserButton = new JButton("Wybierz plik");
	outlookFileChooserButton.addActionListener(new ActionChooseFile());
	this.databaseLabel = new JLabel("Baza danych");
	this.xmlLabel = new JLabel("XML");
	this.outlookLabel = new JLabel("Outlook");
	ioDatabase = ioOutlook = ioXML = false;

	if (isImport)
	    this.infoLabel = new JLabel("Importuj list� z...");
	else
	    this.infoLabel = new JLabel("Eksportuj list� do...");
	infoLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
    }
}
